package editor.modelo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import editor.modelo.Space.Tipo;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import jxl.DateCell;

/**
 * Hello world!
 * 
 */

public class ExcelInterface {

	private static int COL_ID = 0;
	private static int COL_NOMBRE = 1;
	private static int COL_TIPO = 2;

	
	private List<Floor> pisos;
	private File dir;
	
	public static void main(String[] args) {
		try {
			//new ExcelInterface().readFloors();
			new ExcelInterface().writeFloors();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void writeFloors() {
		File dir = ModelUtilities.getWorkingDir();
		List<Floor> pisos = ModelUtilities.loadBuilding(dir, new JSONConverter(
				3), false);

		try {
			WritableWorkbook workbook = Workbook.createWorkbook(new File(dir
					.getAbsolutePath() + "/output.xls"));
			WritableSheet sheet = workbook.createSheet("First Sheet", 0);

			int fila = 0;
			int col = 0;
			Label label;
			for (Floor piso : pisos) {
				col = COL_ID;
				label = new Label(col, fila, "ID_PISO:");
				sheet.addCell(label);

				col = COL_ID + 1;
				label = new Label(col, fila, piso.getFloorName());
				sheet.addCell(label);

				fila++;

				col = COL_ID;
				label = new Label(col, fila, "ID");
				sheet.addCell(label);

				col = COL_NOMBRE;
				label = new Label(col, fila, "NOMBRE");
				sheet.addCell(label);

				col = COL_TIPO;
				label = new Label(col, fila, "TIPO");
				sheet.addCell(label);
				
				col = COL_TIPO+1;
				label = new Label(col, fila, "TIPO");
				sheet.addCell(label);

				fila++;
				for (Space esp : piso.getEspacios()) {
					col = COL_ID;
					label = new Label(col, fila, esp.getId());
					sheet.addCell(label);

					col = COL_NOMBRE;
					label = new Label(col, fila, esp.getName());
					sheet.addCell(label);

					col = COL_TIPO;
					label = new Label(col, fila, esp.getTipo().toString());
					sheet.addCell(label);
					
					col = COL_TIPO+1;
					label = new Label(col, fila, String.valueOf(esp.getTipo().ordinal()));
					sheet.addCell(label);

				

					fila++;
				}

			}

			workbook.write();
			workbook.close();
			System.out.println("Proceso finalizado");

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void readFloors() {
		dir = ModelUtilities.getWorkingDir();
		pisos = ModelUtilities.loadBuilding(dir, new JSONConverter(
				3), false);

		FileInputStream fs = null;
		try {
			fs = new FileInputStream(new File(dir.getAbsolutePath()
					+ "/output.xls"));
			WorkbookSettings ws = new WorkbookSettings();
			ws.setEncoding("ISO-8859-1");
			Workbook workbook = Workbook.getWorkbook(fs, ws);
			
			Sheet s = null;
			Cell rowData[] = null;
			Floor pisoActual = null;
			Space spaceActual = null;
			
			s = workbook.getSheet(0);

			System.out.println("Total Rows inside Sheet:" + s.getRows());

			System.out.println("Total Column inside Sheet:" + s.getColumns());

			int fila = 0;
			int col = 0;
			
			while(true){
				rowData = s.getRow(fila);
				if(rowData[COL_ID].getContents().equals("ID_PISO:")){
					pisoActual = getPiso(rowData[COL_ID+1].getContents());
					fila++;//salteo el titulo
					fila++;
					rowData = s.getRow(fila);
					
					while(!rowData[COL_ID].getContents().equals("ID_PISO:")){
						spaceActual = getEspacio(pisoActual, rowData[COL_ID].getContents());
						
						//System.out.println(rowData[COL_NOMBRE].getContents());

						spaceActual.setName(rowData[COL_NOMBRE].getContents());
					
						if(rowData[COL_TIPO]!= null)
						spaceActual.setTipo(Tipo.valueOf((rowData[COL_TIPO]).getContents()));

						fila++;
						if(fila == s.getRows()) break;
						rowData = s.getRow(fila);
					}
					if(fila == s.getRows()) break;
				}
			}
			workbook.close();
		
			saveFloors();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fs.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void saveFloors() {
		for(Floor p: pisos){
			JSONConverter js = new JSONConverter(3);
			js.toJSONFile(p,  dir + "/" + p.getFloorName()+".json");
		}
		
	}

	private Space getEspacio(Floor piso, String id) {
		for(Space s: piso.getEspacios()){
			if(s.getId().equals(id)) return s;
		}
		return null;
	}

	private Floor getPiso(String id) {
		for(Floor f: pisos){
			if(f.getFloorName().equals(id))
				return f;
		}
		
		return null;
	}


}
